package com.loong.shop.handler;

import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.enumerate.ResultCode;
import com.loong.common.exception.GlobalException;
import com.loong.common.executor.BatchExecutor;
import com.loong.common.param.BaseIdParam;
import com.loong.common.utils.TransformUtils;
import com.loong.shop.business.GoodsBrandBusiness;
import com.loong.shop.common.backend.param.GoodsBaseQueryParam;
import com.loong.shop.common.backend.param.GoodsBrandParam;
import com.loong.shop.common.backend.result.GoodsBaseResult;
import com.loong.shop.common.backend.result.GoodsBrandResult;
import com.loong.shop.common.forehead.result.GoodsBrandForeheadResult;
import com.loong.shop.mapper.GoodsBrandMapper;
import com.loong.shop.model.GoodsBrand;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 处理商品品牌业务的handler
 * @Author: 刘群
 * @Date: 2020/10/27 8:56
 */
@Component
public class GoodsBrandHandler {

    @Resource
    private GoodsBrandBusiness brandBusiness;

    @Resource
    private GoodsBrandMapper brandMapper;

    @Resource
    private GoodsBaseHandler goodsBaseHandler;

    @Resource
    private BatchExecutor batchExecutor;

    /**
     * 后台新增商品品牌
     * @param param 商品品牌封装的参数
     */
    public void saveGoodsBrand(GoodsBrandParam param){
        //查询数据库中该品牌名称是否存在
        List<GoodsBrand> brandList = brandBusiness.findGoodsBrandByParam(param);
        if (CollectionUtils.isEmpty(brandList)){
            //不存在则新增
            param.setCreateTime(new Date());
            brandBusiness.save(TransformUtils.transform(param, GoodsBrand.class));
        }else {
            throw new GlobalException(ResultCode.PARAM_ERROR,"Goods brand already exists");
        }

    }

    /**
     * 后台更新商品品牌
     * @param param 商品品牌封装的参数
     */
    public void updateGoodsBrand(GoodsBrandParam param){
        //查询数据库中该品牌名称是否存在
        List<GoodsBrand> brandList = brandBusiness.findGoodsBrandByParam(param);
        if (CollectionUtils.isEmpty(brandList)){
            //不存在则执行更新
            param.setLastUpdateTime(new Date());
            brandBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param, GoodsBrand.class));
        }else {
            throw new GlobalException(ResultCode.PARAM_ERROR,"Goods brand already exists");
        }
    }

    /**
     * 后台批量删除商品品牌
     * @param idList 商品品牌id集合
     */
    public void deleteGoodsBrand(List<Long> idList){
        batchExecutor.batchExecute(idList,id->id.toString(),(item)->{
            //查询该品牌是否使用在商品中，若有则不能删除，若没有则可以直接删除
            GoodsBaseQueryParam param = GoodsBaseQueryParam.builder().brandId(item).build();
            List<GoodsBaseResult> baseResultList = goodsBaseHandler.findGoodsBaseByParam(param);
            if (CollectionUtils.isEmpty(baseResultList)){
                brandBusiness.deleteById(item);
            }else {
                throw new GlobalException("Goods brand is used");
            }
        });
    }

    /**
     * 后台分页查询商品品牌
     * @param query 查询参数
     * @return 分页结果
     */
    public Pager<GoodsBrandResult> pageQueryGoodsBrand(BaseQuery query){
        Pager<GoodsBrand> pager = brandBusiness.pageQuery(query, () -> brandBusiness.findAll());
        return TransformUtils.transformPage(pager,GoodsBrandResult.class);
    }

    /**
     * 后台根据商品类别查询品牌名称
     * @param categoryId 商品类别id
     * @return 商品品牌集合
     */
    public List<GoodsBrandResult> findBrandByCategoryId(Long categoryId){
        return TransformUtils.transformList(brandBusiness.findGoodsBrandByCategoryId(categoryId),GoodsBrandResult.class);
    }

    /**
     * 前台去重查询商品品牌
     * @return 商品品牌集合
     */
    public List<GoodsBrandForeheadResult> findBrandToForehead(){
       return TransformUtils.transformList(brandMapper.findGoodsBrands(),GoodsBrandForeheadResult.class);
    }

    /**
     * 前台根据商品类别查询品牌名称
     * @param shopId 商品类别id
     * @return 商品品牌集合
     */
    public List<GoodsBrandForeheadResult> findBrandsByCategoryIdToForehead(Long shopId){
        return TransformUtils.transformList(brandBusiness.findGoodsBrandByCategoryId(shopId),GoodsBrandForeheadResult.class);
    }

}
